import time

import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class TestDeployment:

    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/deployment/1_get_deployment_list.yaml'))
    def test_get_deployment_list(self, deploymentinfo, get_env_info):
        # 获取deployment列表接口
        baseUrl = get_env_info
        url = baseUrl + deploymentinfo['request']['url'].replace('{namespace}','default')
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/deployment/2_create_deployment.yaml'))
    def test_create_deployment(self, deploymentinfo, get_env_info):
        # 创建deployment接口
        baseUrl = get_env_info
        url = baseUrl + deploymentinfo['request']['url'].replace('{namespace}','default')
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in deploymentinfo['name']:
            deploymentname=req.json()['metadata']['name']
            globals()['deploymentname']=deploymentname
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/deployment/3_get_specific_deployment.yaml'))
    def test_get_specific_deployment(self, deploymentinfo, get_env_info):
        # 获取指定deployment接口
        baseUrl = get_env_info
        url = baseUrl + deploymentinfo['request']['url'].replace('{namespace}','default').replace('{deploymentname}',globals()['deploymentname'])
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/deployment/4_update_deployment.yaml'))
    def test_update_deployment(self, deploymentinfo, get_env_info):
        # 更新指定deployment接口
        baseUrl = get_env_info
        url = baseUrl + deploymentinfo['request']['url'].replace('{namespace}','default').replace('{deploymentname}',globals()['deploymentname'])
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/deployment/5_update_section_deployment.yaml'))
    def test_update_section_deployment(self, deploymentinfo, get_env_info):
        # 部分更新指定deployment的接口
        baseUrl = get_env_info
        url = baseUrl + deploymentinfo['request']['url'].replace('{namespace}','default').replace('{deploymentname}',globals()['deploymentname'])
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        header = deploymentinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/deployment/6_delete_specific_deployment.yaml'))
    def test_delete_specific_deployment(self, deploymentinfo, get_env_info):
        # 删除deployment接口
        baseUrl = get_env_info
        url = baseUrl + deploymentinfo['request']['url'].replace('{namespace}','default').replace('{deploymentname}',globals()['deploymentname'])
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']